package com.test.storage.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.storage.entity.Storage;
import com.test.storage.mapper.StorageMapper;
import com.test.storage.service.StorageService;
import org.springframework.stereotype.Service;

@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService {
    @Override
    public void deduct(String commodityCode, Integer count) {
        Integer currentCount = getOne(Wrappers.<Storage>lambdaQuery()
                .select(Storage::getCount)
                .eq(Storage::getCommodityCode, commodityCode))
                .getCount();
        if (count <= currentCount) {
            update(Wrappers.<Storage>lambdaUpdate()
                    .set(Storage::getCount, currentCount - count)
                    .eq(Storage::getCommodityCode, commodityCode));
        } else {
            throw new RuntimeException("库存不足...");
        }
    }
}
